﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Admin;
using WebHoiChoMuaBan.Controllers.Utility;
using System.IO;
using Newtonsoft.Json;
using System.Data;

namespace WebHoiChoMuaBan.Controllers
{
    public class CreateCategoryController : AdminController
    {

        public ActionResult Index(int? id)
        {
            //Get pages from db using linq query
            PageTypeHelper pageTypeHelper = new PageTypeHelper();
            ViewData["Pages"] = pageTypeHelper.getAllPageTypes();
            //Reset selectedpage when reload
            Session["SelectedPage"] = null;
            // Push pages into ViewData to show 

            return View("~/Views/Admin/CreateCategory.aspx");
        }

        public RedirectResult addCategory(FormCollection form)
        {
            //tạo danh mục cấp 1
            if (form.Keys.Count == 3)
            {
                Category cate = new Category();
                cate.CateName = form.Get("txtCateName");
                cate.PageId = int.Parse(form.Get("ddlCateLevel1"));
                foreach (string file in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                    ImageHelper imgHelper = new ImageHelper();
                    string fileName = hpf.FileName;
                    string path = Path.Combine(Server.MapPath("~/Content/Images/Upload/"), hpf.FileName);
                    cate.CateIcon = fileName;
                    imgHelper.uploadImage(path, hpf, 200, 200);

                }
                new CategoryHelper().Add(cate);
            }
            else
            {
                //tạo danh mục cấp 2
                Category cate = new Category();
                cate.CateName = form.Get("txtCateName");
                cate.ParentCateId = int.Parse(form.Get("ddlCateLevel2"));
                foreach (string file in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                    ImageHelper imgHelper = new ImageHelper();
                    string fileName = hpf.FileName;
                    string path = Path.Combine(Server.MapPath("~/Content/Images/Upload/"), hpf.FileName);
                    cate.CateIcon = fileName;
                    imgHelper.uploadImage(path, hpf, 200, 200);

                }
                new CategoryHelper().Add(cate);
            }

            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }

        [HttpPost]
        public string deleteCategory(string cateId)
        {
            return new CategoryHelper().Delete(int.Parse(cateId));
        }

        [HttpPost]
        public string getCategoryInfo(string cateId)
        {
            Category cate = new CategoryHelper().getCate(int.Parse(cateId));
            DataTable dt = new DataTable();
            dt.Columns.Add("CateId");
            dt.Columns.Add("CateName");
            dt.Columns.Add("PageId");
            dt.Columns.Add("ParrentCateId");
            dt.Columns.Add("CateIcon");
            dt.Columns.Add("CateCount");
            dt.Rows.Add(dt.NewRow());
            dt.Rows[0]["CateId"] = cate.CateId;
            dt.Rows[0]["CateName"] = cate.CateName;
            dt.Rows[0]["PageId"] = cate.PageId;
            dt.Rows[0]["ParrentCateId"] = cate.ParentCateId;
            dt.Rows[0]["CateIcon"] = cate.CateIcon;
            dt.Rows[0]["CateCount"] = cate.CateCount;
            string json = JsonConvert.SerializeObject(dt);
            return json;
        }
        [HttpPost]
        public ActionResult editCategory(FormCollection form)
        {
            //update danh mục thành danh mục cấp 1
            if (form.Keys.Count == 4)
            {
                Category cate = new CategoryHelper().getCate(int.Parse(form.Get("ModalID")));
                cate.PageId = int.Parse(form.Get("ddlModalCateLevel1"));
                new CategoryHelper().Update(cate);
                return View("~/Views/Admin/CreateCategory.aspx");
            }
            else
            {
                Category cate = new CategoryHelper().getCate(int.Parse(form.Get("ModalID")));
                cate.PageId = null;
                cate.ParentCateId = int.Parse(form.Get("ddlModalCateLevel2"));
                new CategoryHelper().Update(cate);
                return View("~/Views/Admin/CreateCategory.aspx");
            }
        }
    }
}
